package com.atguigu.gmall.product.controller;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/15 14:16
 * @Version 1.0
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture.runAsync(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
        //  复制小括号，写死右箭头，落地大括号
        //        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //            System.out.println("我凑是不会，你能咋的!");
        //        });
        //        System.out.println(completableFuture.get());
        //
        //                CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
        //                            System.out.println("有返回值");
        //                            //  int i = 1 / 0;
        //                            return 404;
        //                        }).whenComplete((t, u) -> {
        //                            System.out.println("t:\t" + t);
        //                            System.out.println("u:\t" + u);
        //                        }).thenApplyc((tt) -> {
        //                            System.out.println("tt:\t" +tt); // 404
        //                            return 2 * tt;
        //                        }).thenAccept(s->{
        //                            System.out.println(s);
        //                        })
        //                        .exceptionally((s) -> {
        //                            System.out.println(s);
        //                            return 101;
        //                        });
        //        System.out.println(future.get());

        //        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //            System.out.println("呵呵");
        //        }).thenAccept(s -> {
        //            System.out.println(s);
        //        });
        //
        //        System.out.println(completableFuture.get());

        //        CompletableFuture<Void> completableFuture1 = CompletableFuture.supplyAsync(() -> {
        //            return 101;
        //        }).thenAccept(s -> {
        //            System.out.println(s); // 101;
        //        }).thenRun(()->{
        //            System.out.println("over"); // 最后执行
        //        });
        //
        //        System.out.println(completableFuture1.get());

        //  并行化：应该使用异步方法、
        //  创建线程A
        CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> "hello");

        //  创建线程B
        CompletableFuture<Void> futureB = futureA.thenAcceptAsync(c -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(c + ":\tB");
        });
        //  创建线程C
        CompletableFuture<Void> futureC = futureA.thenAcceptAsync(c -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(c + ":\tC");
        });

        //        System.out.println(futureB.get());
        //        System.out.println(futureC.get());

        //  等待所有
//        CompletableFuture.allOf(futureA,futureB,futureC).join();
        CompletableFuture.anyOf(futureA,futureB,futureC).join();
        System.out.println("over----");

    }
}
